/**
 * Created by jimmy on 2017/6/10.
 */
var constUtil = require("../utils/ConstUtil");
var orderService = require("../service/OrderService");
var commService = require("../service/CommService");
var async = require("async");

exports.index = function (req,res,next) {

    async.series({
       ordSum:function (callbk) {
        orderService.getOrdSum(req.session.openid,function (err,rows) {
            var param = {};
            for(var i in rows){
                param['type'+rows[i].status] = rows[i].cnt;
            }
            callbk(null,param);
        });
       },
        user:function (callbk) {
            var user = {
                headimg:req.session.head_img,
                name:req.session.nickname,
                userLevel:constUtil.levelName(4),
                openid:req.session.openid
            };
            callbk(null,user);
        },
        comm:function (callbk) {
            commService.getCommSumByState(req.session.openid,callbk);
        }
    },function (err,result) {
        res.render("usercenter",{title:"个人中心",home:"",prod:"",prebuy:"",my:"on",user:result.user,ordsum:result.ordSum,commTotal:result.comm[0].comm});
    });



};

exports.getUserPd = function (req,res,next) {
    var typeId = req.query.typeId;
    async.series({
        ordInfo:function (callbk) {
            orderService.getOrder(req.session.openid,typeId,callbk);
        },
        freight:function (callbk) {
            orderService.getOrderSendFee(req.session.openid,typeId,callbk);
        }
    },function (err,result) {
        var totalmoney = 0;
        var totalsum = 0;
        var ords = result.ordInfo;
        for (var i in ords){
            console.log(Number(ords[i].prod_price)+"@@@@@@@@@@@@@@@@@@@@@"+Number(ords[i].prod_sum));
            totalmoney += Number(ords[i].prod_price)*Number(ords[i].prod_sum);
            totalsum += Number(ords[i].prod_sum);
        }
        totalmoney+result.freight[0].send_fee;
        res.render("mydel",{title:"我的交易",dealtype:typeId,prods:result.ordInfo,freight:result.freight,totalmoney:totalmoney,totalsum:totalsum});
    });
}

exports.getUserPdList = function (req,res,next) {
    var typeId = req.query.typeId;
    async.series({
        ordInfo:function (callbk) {
            orderService.getOrder(req.session.openid,typeId,callbk);
        },
        freight:function (callbk) {
            orderService.getOrderSendFee(req.session.openid,typeId,callbk);
        }
    },function (err,result) {
        var totalmoney = 0;
        var totalsum = 0;
        var ords = result.ordInfo;
        for (var i in ords){
            console.log(Number(ords[i].prod_price)+"@@@@@@@@@@@@@@@@@@@@@"+Number(ords[i].prod_sum));
            totalmoney += Number(ords[i].prod_price)*Number(ords[i].prod_sum);
            totalsum += Number(ords[i].prod_sum);
        }
        totalmoney+result.freight[0].send_fee;
        res.status(200).end(JSON.stringify({dealtype:typeId,prods:result.ordInfo,freight:result.freight,totalmoney:totalmoney,totalsum:totalsum}));
    });
}